<?php


namespace app\admin\model;

use think\Db;
use think\Model;

class Category extends BaseModel
{
    public function children()
    {
        return $this->hasMany('Category', 'parent_id', 'id');
    }

    public function topicImg()
    {
        return $this->belongsTo('Image', 'topic_img_id', 'id');
    }

    public static function getRawAll()
    {
//        return self::with('topicImg')->select();
        return Db::query("select c.*, i.id as image_id, i.url, i.from from category as c left join image as i on c.topic_img_id = i.id");
    }

    public static function getAll($parent_id, $page=1, $size=15)
    {
        return self::with(['children', 'children.topicImg'])->where('parent_id', '=',$parent_id)->paginate($size, true, ['page' => $page]);
    }
}